﻿
@{
    ViewBag.Title = "用户登录";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

<div class="main layui-clear">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>登录</legend>
    </fieldset>
    <div class="layui-form layui-form-pane">
        <form method="post">
            @Html.AntiForgeryToken()
            <div class="layui-form-item">
                <label for="L_email" class="layui-form-label">工号</label>
                <div class="layui-input-inline">
                    <input type="text" id="StaffNo" name="StaffNo" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="L_pass" name="pass" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-normal" lay-filter="login" lay-submit>立即登录</button>
            </div>
        </form>
    </div>
</div>
@section scripts{
    <script>
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer
                , $ = layui.jquery
                , form = layui.form

            var returnUrl = window.location.search.split("%2f");
            if (returnUrl.length > 1) {
                returnUrl = returnUrl[1];
            } else {
                returnUrl = null;
            }
           
            form.on('submit(login)', function (data) {
                $.post('@Url.Action("Login", "Account")', data.field, function (res) {
                    if (!res.success) {
                        layer.msg(res.message, { icon: 2 });
                    } else {
                        layer.msg(res.message, { icon: 1,time:1000 }, function (index) {
                            var user = res.data.user;
                            layui.data('user', {
                                key: 'user',
                                value: {
                                    username: user.StaffName
                                    , uid: user.Id
                                    , avatar: user.HeadPortrait
                                    , experience: user.Integral
                                    , IsAdmin: user.IsAdmin
                                    , sex: user.Gender === "1" ? "男" : "女"
                                }
                            });                            
                            layer.close(index);
                            location.href = returnUrl !== null ? location.origin + "/"+returnUrl: '@Url.Action("Index", "Account")';
                        });
                    }
                }, 'json');

                return false;
            });
        });

    </script>
}